<template>
	<el-form size="large" class="login-content-form">
		<el-form-item class="login-animation1">
			<el-input text :placeholder="$t('message.account.accountPlaceholder1')" v-model="state.ruleForm.userName"
				clearable autocomplete="off">
				<template #prefix>
					<el-icon class="el-input__icon"><ele-User /></el-icon>
				</template>
			</el-input>
		</el-form-item>
		<el-form-item class="login-animation2">
			<el-input :type="state.isShowPassword ? 'text' : 'password'"
				:placeholder="$t('message.account.accountPlaceholder2')" v-model="state.ruleForm.password" autocomplete="off">
				<template #prefix>
					<el-icon class="el-input__icon"><ele-Unlock /></el-icon>
				</template>
				<template #suffix>
					<i class="iconfont el-input__icon login-content-password"
						:class="state.isShowPassword ? 'icon-yincangmima' : 'icon-xianshimima'"
						@click="state.isShowPassword = !state.isShowPassword">
					</i>
				</template>
			</el-input>
		</el-form-item>
		<el-form-item class="login-animation3">
			<div class="rf">
				<div class="remember" :class="{ active: isRemember }" @click="isRemember = !isRemember"><span></span>
					记住密码</div>
				<div class="forget" @click="alertShow = 1">忘记密码?</div>
			</div>
		</el-form-item>

		<el-form-item class="login-animation4">
			<el-button type="primary" class="login-content-submit" round v-waves @click="onSignIn"
				:loading="state.loading.signIn">
				<span>{{ $t('message.account.accountBtnText') }}</span>
			</el-button>
		</el-form-item>
		<!-- <el-form-item class="login-animation5">
			<div class="login-warning">
				<div class="to-agree" :class="{ agree: isAgree }" @click="isAgree = !isAgree"></div>
				<div class="text">
					我已阅读并同意供应商管理系统 <a @click="alertWindow(2)">用户协议</a>
				</div>
			</div>
		</el-form-item> -->
	</el-form>
	<div class="alert-window" v-if="alertShow > 0">
		<div class="win-password" v-if="alertShow === 1">
			<div class="title">
				<b>修改密码</b>
				<div class="close" @click="alertShow = 0"></div>
			</div>
			<div class="btitle">忘记密码及修改密码</div>
			<div class="ntitle">请联系管理员或致电客服</div>
			<div class="confirm" @click="alertShow = 0">好的</div>
		</div>
		<!-- <div class="win-agreement" v-if="alertShow === 2">
			<div class="agree-con">
				<div class="title">用户协议</div>
				<div class="agree-content">
					<p>Lorem, ipsum dolor sit amet consectetur adipisicing elit. Alias iusto, ab recusandae, incidunt
						mollitia
						quas eligendi voluptatibus totam aliquid ipsam quaerat ipsa soluta repellendus, cumque autem a
						dignissimos
						unde. Et!</p>
				</div>
				<div class="btns">
					<div class="close" @click="alertShow = 0">返回</div>
					<div class="agree" @click="agreeCb()">同意</div>
				</div>
			</div>
		</div> -->
	</div>
</template>

<script setup lang="ts" name="loginAccount">
import { reactive, computed, ref } from 'vue';
import { useRoute, useRouter } from 'vue-router';
import { ElMessage } from 'element-plus';
import { useI18n } from 'vue-i18n';
import Cookies from 'js-cookie';
// import { storeToRefs } from 'pinia';
// import { useThemeConfig } from '/@/stores/themeConfig';
// import { initFrontEndControlRoutes } from '/@/router/frontEnd';
// import { initBackEndControlRoutes } from '/@/router/backEnd';
import { Session } from '/@/utils/storage';
import { formatAxis } from '/@/utils/formatTime';
import { NextLoading } from '/@/utils/loading';
import { useLoginApi } from '/@/api/login';

// 定义变量内容
const { t } = useI18n();
// const storesThemeConfig = useThemeConfig();
// const { themeConfig } = storeToRefs(storesThemeConfig);
const route = useRoute();
const router = useRouter();
const state = reactive({
	isShowPassword: false,
	ruleForm: {
		userName: '',
		password: '',
	},
	loading: {
		signIn: false,
	},
});
const isRemember = ref(false);
const alertShow = ref(0);
// const isAgree = ref(false);
// function alertWindow(i: number) {
// 	alertShow.value = i;
// }
// function agreeCb() {
// 	isAgree.value = true
// 	alertShow.value = 0;
// }
// 时间获取
const currentTime = computed(() => {
	return formatAxis(new Date());
});
// 登录
const onSignIn = async () => {


	if (state.ruleForm.userName === '') {
		ElMessage('请输入用户名')
		return false;
	}

	if (!(state.ruleForm.password)) {
		ElMessage('请输入密码')
		return false;
	}
	// if (!(isAgree.value)) {
	// 	ElMessage('请阅读并同意供应商管理系统《用户协议》')
	// 	return false;
	// }

	const res = await useLoginApi().signIn({ phone: state.ruleForm.userName, password: state.ruleForm.password });
	if (res) {
		// 存储 token 到浏览器缓存
		Session.set('token', res.data.Authorization);
		// 模拟数据，对接接口时，记得删除多余代码及对应依赖的引入。用于 `/src/stores/userInfo.ts` 中不同用户登录判断（模拟数据）
		Cookies.set('userName', state.ruleForm.userName);
		signInSuccess(false);
		// if (!themeConfig.value.isRequestRoutes) {
		// 	// 前端控制路由，2、请注意执行顺序
		// 	const isNoPower = await initFrontEndControlRoutes();
		// 	signInSuccess(isNoPower);
		// } else {
		// 	// 模拟后端控制路由，isRequestRoutes 为 true，则开启后端控制路由
		// 	// 添加完动态路由，再进行 router 跳转，否则可能报错 No match found for location with path "/"
		// 	const isNoPower = await initBackEndControlRoutes();
		// 	// 执行完 initBackEndControlRoutes，再执行 signInSuccess
		// 	signInSuccess(isNoPower);
		// }
	}
};
// 登录成功后的跳转
const signInSuccess = (isNoPower: boolean | undefined) => {
	if (isNoPower) {
		ElMessage.warning('抱歉，您没有登录权限');
		Session.clear();
	} else {
		// 初始化登录成功时间问候语
		let currentTimeInfo = currentTime.value;
		// 登录成功，跳到转首页
		// 如果是复制粘贴的路径，非首页/登录页，那么登录成功后重定向到对应的路径中
		if (route.query?.redirect) {
			router.push({
				path: <string>route.query?.redirect,
				query: Object.keys(<string>route.query?.params).length > 0 ? JSON.parse(<string>route.query?.params) : '',
			});
		} else {
			router.push('/home');
		}
		// 登录成功提示
		const signInText = t('message.signInText');
		ElMessage.success(`${currentTimeInfo}，${signInText}`);
		// 添加 loading，防止第一次进入界面时出现短暂空白
		NextLoading.start();
	}
	state.loading.signIn = false;
};
</script>

<style scoped lang="scss">
.login-content-form {
	margin-top: 20px;

	@for $i from 1 through 5 {
		.login-animation#{$i} {
			opacity: 0;
			animation-name: error-num;
			animation-duration: 0.5s;
			animation-fill-mode: forwards;
			animation-delay: calc($i/10) + s;
		}
	}

	.login-content-password {
		display: inline-block;
		width: 20px;
		cursor: pointer;

		&:hover {
			color: #909399;
		}
	}

	.login-content-code {
		width: 100%;
		padding: 0;
		font-weight: bold;
		letter-spacing: 5px;
	}

	.login-content-submit {
		width: 100%;
		letter-spacing: 2px;
		font-weight: 300;
		margin-top: 15px;
	}
}

.alert-window {
	width: 100%;
	height: 100%;
	position: fixed;
	z-index: 10;
	left: 0;
	top: 0;
	background: rgba(0, 0, 0, 0.6);

}

.win- {
	&password {
		width: 340px;
		height: 200px;
		margin: auto;
		position: absolute;
		left: 0;
		right: 0;
		bottom: 0;
		top: 0;
		background: rgb(255, 255, 255);
		border-radius: 6px;

		.title {
			display: flex;
			height: 60px;
			width: 90%;
			margin: 0 auto 3px;
			color: rgb(38, 50, 56);
			align-items: center;
			justify-content: space-between;

			b {
				font-size: 16px;
				font-weight: bold;
			}

			.close {
				width: 28px;
				height: 28px;
				background: url(../assets/images/g26.png) no-repeat center;
				background-size: 100%;

			}
		}

		.btitle {
			font-size: 18px;
			font-weight: bold;
			width: 100%;
			text-align: center;
			color: rgb(54, 145, 255);
			margin-bottom: 2px;

		}

		.ntitle {
			font-size: 14px;
			color: rgb(38, 50, 56);
			height: 28px;
			line-height: 28px;
			text-align: center;
			margin-bottom: 25px;
		}

		.confirm {
			width: 112px;
			height: 36px;
			text-align: center;
			color: rgb(255, 255, 255);
			align-items: center;
			text-align: center;
			justify-content: center;
			background: rgb(54, 145, 255);
			text-align: center;
			line-height: 36px;
			border-radius: 4px;
			margin: 0 auto;
			cursor: pointer;
		}
	}

	&agreement {
		width: 340px;
		height: 520px;
		margin: auto;
		position: absolute;
		left: 0;
		right: 0;
		bottom: 0;
		top: 0;
		background: rgb(255, 255, 255);
		border-radius: 6px;

		.title {
			height: 50px;
			line-height: 60px;
			width: 90%;
			margin: 0 auto 3px;
			color: rgb(38, 50, 56);
			text-align: center;
			font-size: 20px;
			font-weight: bold;
			color: black;
			text-align: center;
		}

		.agree-content {
			width: 98%;
			margin: 0 auto 20px;
			height: 390px;
			overflow: auto;
			box-sizing: border-box;
			padding: 0 12px;
		}

		.btns {
			height: 36px;
			display: flex;
			align-items: center;
			justify-content: center;

			div {
				width: 112px;
				height: 36px;
				text-align: center;
				color: rgb(255, 255, 255);
				align-items: center;
				text-align: center;
				justify-content: center;
				background: rgb(54, 145, 255);
				text-align: center;
				line-height: 34px;
				border-radius: 4px;
				margin: 0 16px;
				box-sizing: border-box;
				border: 1px solid rgb(54, 145, 255);

				&.close {
					border-color: rgb(140, 140, 140);
					color: rgb(140, 140, 140);
					background: none;
				}
			}
		}
	}
}

.rf {
	width: 100%;
	margin: 0 auto;
	height: 24px;
	display: flex;
	align-items: center;
	justify-content: space-between;

	.remember {
		cursor: pointer;
		color: rgb(140, 140, 140);
		font-size: 12px;
		display: flex;
		align-items: center;
		justify-content: flex-start;

		&.active span::before {
			content: "";
			display: block;
			width: 10px;
			height: 10px;
			background: rgb(0, 118, 255);
			border-radius: 50%;
		}

		span {
			margin-right: 5px;
			width: 16px;
			height: 16px;
			border: 1px solid rgb(199, 199, 205);
			border-radius: 50%;
			display: flex;
			align-items: center;
			justify-content: center;
		}
	}

	.forget {
		cursor: pointer;
		text-align: right;
		font-size: 12px;
		color: rgb(54, 145, 255);
	}
}

.login-warning {
	width: 100%;
	margin: 0 auto;
	display: flex;
	align-items: center;
	justify-content: center;
	height: 24px;

	.to-agree {
		box-sizing: border-box;
		margin-right: 5px;
		width: 16px;
		height: 16px;
		border: 1px solid rgb(199, 199, 205);
		border-radius: 50%;
		display: flex;
		align-items: center;
		justify-content: center;

		&.agree::before {
			content: "";
			display: block;
			width: 10px;
			height: 10px;
			background: rgb(0, 118, 255);
			border-radius: 50%;
		}
	}

	.text {
		font-size: 12px;
		color: rgb(140, 140, 140);

		a {
			color: rgb(0, 118, 255);
			text-decoration: underline;
		}
	}
}
</style>
